package com.bmadmin.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SxdlDomain;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.domain.TysxDomain;
import com.bmadmin.domain.TysxFlwhDomain;
import com.bmadmin.service.TysxFlwhService;
import com.bmadmin.service.TysxService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/tysxflwh")
public class TysxFlwhController {
private Logger logger = LogManager.getLogger(this.getClass().getName());
	
    @Autowired
    TysxFlwhService flwhService;
    
    /**
     * 获取通用事项大类列表
     */
	@GetMapping("/getTysxDlList")
    public Object getTysxDlList(HttpSession session,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	

		PageHelper.startPage(page, limit);
		List<TysxFlwhDomain> list = flwhService.getTysxDlList();
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    @PostMapping("/insertSxDl")
    public NormalResult insertSxDl(HttpSession session,TysxFlwhDomain tysxflwh) {
    	String log = "通用事项大类新增>>>>>>>>>>>";
    	logger.info(log+"参数【"+tysxflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(tysxflwh.getSxdl_mc())){
        		result.setCode("9999");
                result.setMsg("事项大类名称为空！");
                logger.info(log+"事项大类名称为空！");
                return result;
        	}
        	int retResult = flwhService.insertSxDl(tysxflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("事项大类保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    @PostMapping("/deleteSxdl")
    public NormalResult deleteSxdl(String sxdl_dm) {
    	String log = "事项大类删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sxdl_dm)) {
            result.setCode("9999");
            result.setMsg("传入ID为空!");
            return result;
        }

        int retResult = flwhService.deleteSxdl(sxdl_dm);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/updateSxDl")
    public NormalResult updateSxDl(HttpSession session,TysxFlwhDomain tysxflwh) {
    	String log = "通用事项大类更新>>>>>>>>>>>";
    	logger.info(log+"参数【"+tysxflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(tysxflwh.getSxdl_dm())){
        		result.setCode("9999");
                result.setMsg("事项大类代码为空！");
                logger.info(log+"事项大类代码为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(tysxflwh.getSxdl_mc())){
        		result.setCode("9999");
                result.setMsg("事项大类名称为空！");
                logger.info(log+"事项大类名称为空！");
                return result;
        	}
        	int retResult = flwhService.updateSxDl(tysxflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("更新成功！");
                logger.info(log+"更新成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("更新失败！");
                logger.info(log+"更新失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("事项大类更新异常！");
            logger.error(e);
        }
        return result;
    }
	
    /**
     * 获取通用事项小类列表
     */
	@GetMapping("/getTysxXlList")
    public Object getTysxXlList(HttpSession session,TysxFlwhDomain tysxflwh,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
    	map.put("sxdl_dm", tysxflwh.getSxdl_dm());
		PageHelper.startPage(page, limit);
		List<TysxFlwhDomain> list = flwhService.getTysxXlList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    @PostMapping("/insertSxXl")
    public NormalResult insertSxXl(HttpSession session,TysxFlwhDomain tysxflwh) {
    	String log = "通用事项小类新增>>>>>>>>>>>";
    	logger.info(log+"参数【"+tysxflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(tysxflwh.getSxdl_dm())){
        		result.setCode("9999");
                result.setMsg("事项大类ID为空！");
                logger.info(log+"事项大类ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(tysxflwh.getSxxl_mc())){
        		result.setCode("9999");
                result.setMsg("事项小类名称为空！");
                logger.info(log+"事项小类名称为空！");
                return result;
        	}
        	int retResult = flwhService.insertSxXl(tysxflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("事项小类保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    @PostMapping("/deleteSxxl")
    public NormalResult deleteSxxl(String sxxl_dm) {
    	String log = "事项小类删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sxxl_dm)) {
            result.setCode("9999");
            result.setMsg("传入ID为空!");
            return result;
        }

        int retResult = flwhService.deleteSxxl(sxxl_dm);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/updateSxxl")
    public NormalResult updateSxxl(HttpSession session,TysxFlwhDomain tysxflwh) {
    	String log = "通用事项小类更新>>>>>>>>>>>";
    	logger.info(log+"参数【"+tysxflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(tysxflwh.getSxdl_dm())){
        		result.setCode("9999");
                result.setMsg("事项大类代码为空！");
                logger.info(log+"事项大类代码为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(tysxflwh.getSxxl_dm())){
        		result.setCode("9999");
                result.setMsg("事项小类代码为空！");
                logger.info(log+"事项小类代码为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(tysxflwh.getSxxl_mc())){
        		result.setCode("9999");
                result.setMsg("事项小类名称为空！");
                logger.info(log+"事项小类名称为空！");
                return result;
        	}
        	int retResult = flwhService.updateSxxl(tysxflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("更新成功！");
                logger.info(log+"更新成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("更新失败！");
                logger.info(log+"更新失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("事项小类更新异常！");
            logger.error(e);
        }
        return result;
    }
	
    /**
     * 获取通用事项小类列表
     */
	@GetMapping("/getTysxXlUserList")
    public Object getTysxXlUserList(HttpSession session,TysxFlwhDomain tysxflwh,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
    	map.put("sxxl_dm", tysxflwh.getSxxl_dm());
		PageHelper.startPage(page, limit);
		List<TysxFlwhDomain> list = flwhService.getTysxXlUserList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    @PostMapping("/insertSxXlUser")
    public NormalResult insertSxXlUser(HttpSession session,TysxFlwhDomain tysxflwh) {
    	String log = "通用事项小类人员新增>>>>>>>>>>>";
    	logger.info(log+"参数【"+tysxflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(tysxflwh.getSxxl_dm())){
        		result.setCode("9999");
                result.setMsg("事项小类ID为空！");
                logger.info(log+"事项小类ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(tysxflwh.getU_id())){
        		result.setCode("9999");
                result.setMsg("人员ID为空！");
                logger.info(log+"人员ID为空！");
                return result;
        	}
        	int retResult = flwhService.insertSxXlUser(tysxflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("事项小类人员保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    @PostMapping("/deleteSxxlUser")
    public NormalResult deleteSxxlUser(String id) {
    	String log = "事项小类删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(id)) {
            result.setCode("9999");
            result.setMsg("传入ID为空!");
            return result;
        }

        int retResult = flwhService.deleteSxxlUser(id);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/updateSxxlUser")
    public NormalResult updateSxxlUser(HttpSession session,TysxFlwhDomain tysxflwh) {
    	String log = "通用事项小类人员更新>>>>>>>>>>>";
    	logger.info(log+"参数【"+tysxflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(tysxflwh.getSxxl_dm())){
        		result.setCode("9999");
                result.setMsg("事项小类代码为空！");
                logger.info(log+"事项小类代码为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(tysxflwh.getU_id())){
        		result.setCode("9999");
                result.setMsg("事项小类人员代码为空！");
                logger.info(log+"事项小类人员代码为空！");
                return result;
        	}
        	int retResult = flwhService.updateSxxlUser(tysxflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("更新成功！");
                logger.info(log+"更新成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("更新失败！");
                logger.info(log+"更新失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("事项小类人员更新异常！");
            logger.error(e);
        }
        return result;
    }
}
